package com.lxmes.business.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lxmes.base.domain.JcIteminfo;
import com.lxmes.base.mapper.JcIteminfoMapper;
import com.lxmes.business.domain.CcCaigouDetail;
import com.lxmes.business.domain.CcRukuDetail;
import com.lxmes.business.domain.CcRukuMain;
import com.lxmes.business.mapper.CcCaigouDetailMapper;
import com.lxmes.business.mapper.CcRukuDetailMapper;
import com.lxmes.business.mapper.CcRukuMainMapper;
import com.lxmes.common.annotation.DataScope;
import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.NewNumber;
import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.system.domain.SysProcess;
import com.lxmes.system.mapper.SysProcessMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.business.mapper.CcCaigouMainMapper;
import com.lxmes.business.domain.CcCaigouMain;
import com.lxmes.business.service.ICcCaigouMainService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 采购Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-06-11
 */
@Service
public class CcCaigouMainServiceImpl implements ICcCaigouMainService 
{
    @Autowired
    private CcCaigouMainMapper ccCaigouMainMapper;
    @Autowired
    private CcCaigouDetailMapper detailMapper;
    @Autowired
    private SysProcessMapper processMapper;
    @Autowired
    private CcRukuDetailMapper ccRukuDetailMapper;
    @Autowired
    private CcRukuMainMapper ccRukuMainMapper;
    @Autowired
    private JcIteminfoMapper jcIteminfoMapper;

    /**
     * 查询采购
     * 
     * @param id 采购主键
     * @return 采购
     */
    @Override
    public CcCaigouMain selectCcCaigouMainById(Long id)
    {
        return ccCaigouMainMapper.selectCcCaigouMainById(id);
    }
    /**
     * 查询采购
     *
     * @param danhao 采购单号
     * @return 采购
     */
    @Override
    public CcCaigouMain selectCcCaigouMainByDanHao(String danhao) {
        return ccCaigouMainMapper.selectCcCaigouMainByDanHao(danhao);
    }

    /**
     * 查询采购列表
     * 
     * @param ccCaigouMain 采购
     * @return 采购
     */
    @Override
    @DataScope(deptAlias = "de", userAlias = "de")
    public List<CcCaigouMain> selectCcCaigouMainList(CcCaigouMain ccCaigouMain)
    {
        return ccCaigouMainMapper.selectCcCaigouMainList(ccCaigouMain);
    }

    /**
     * 新增采购
     * 
     * @param ccCaigouMain 采购
     * @return 结果
     */
    @Override
    @Transactional
    public int insertCcCaigouMain(CcCaigouMain ccCaigouMain)
    {
        ccCaigouMain.setCreateTime(DateUtils.getNowDate());
        ccCaigouMain.setCreateBy(SecurityUtils.getUsername());
        ccCaigouMain.setDeptId(SecurityUtils.getDeptId());
        ccCaigouMain.setShenqingDate(DateUtils.getNowDate());
        ccCaigouMain.setShenqingName(SecurityUtils.getUsername());
        ccCaigouMain.setShenqingTel(SecurityUtils.getLoginUser().getUser().getPhonenumber());
        ccCaigouMain.setDeptId(SecurityUtils.getDeptId());
        ccCaigouMain.setShenheState(0L);
        ccCaigouMain.setCaigouState("0");
        String number= NewNumber.createNewNumber(ccCaigouMainMapper.getNewCode(DateUtils.getDate()));
        ccCaigouMain.setCaigouDanhao("CG-"+number);
        List<CcCaigouDetail> list = ccCaigouMain.getInfoList();
        if(list.size()>0){
            for ( CcCaigouDetail info:list) {
                info.setCaigouDanhao("CG-"+number);
                BigDecimal cgNum = info.getCaigouNum();
                //info.setTotalPrice(info.getUnitPrice().multiply(cgNum).setScale(2,BigDecimal.ROUND_HALF_UP));
                info.setTotalPrice(new BigDecimal("0.00"));
                info.setCreateTime(DateUtils.getNowDate());
                info.setCreateBy(SecurityUtils.getUsername());
                info.setCaigouDate(ccCaigouMain.getCaigouDate());
                JcIteminfo itemInfo = jcIteminfoMapper.selectJcIteminfoByItemCode(info.getItemCode());
                if(itemInfo.getPrice()!=null){
                    info.setCankaoPrice(itemInfo.getPrice());
                }else {
                    info.setCankaoPrice(new BigDecimal("0.00"));
                }
                Map<String, String> map=new HashMap<>();
                map = detailMapper.getLastCaiGouItem(info.getItemCode());
                if(StringUtils.isNotEmpty(map)){
                    if(map.get("lastPrice")!=null){
                        info.setLastPrice(new BigDecimal(String.valueOf(map.get("lastPrice"))));
                    }
                    if(map.get("caigouDate")!=null){
                        info.setLastDate(DateUtils.dateTime(DateUtils.YYYY_MM_DD,String.valueOf(map.get("caigouDate"))));
                    }

                }else {
                    info.setLastPrice(new BigDecimal("0.00"));
                }
                detailMapper.insertCcCaigouDetail(info);
            }
        }else {
            throw new ServiceException("物料不存在！");
        }
        Map<String, String> map = detailMapper.tJPartsPurchaseInfo("CG-" + number);
        //ccCaigouMain.setTotalPrice(new BigDecimal(String.valueOf(map.get("totalPrice"))));
        ccCaigouMain.setTotalPrice(new BigDecimal("0.00"));
        ccCaigouMain.setTotalNum(new BigDecimal(String.valueOf(map.get("cgNum"))));
        ccCaigouMainMapper.insertCcCaigouMain(ccCaigouMain);
       /* SysProcess process =new SysProcess();
        process.setProcessId(ccCaigouMain.getCaigouDanhao());
        process.setProcessState("0");
        process.setRemark("采购单发起申请");
        process.setCreateBy(SecurityUtils.getUsername());
        process.setCreateTime(DateUtils.getNowDate());
        processMapper.insertSysProcess(process);*/
        return 1;
    }

    /**
     * 修改采购
     * 
     * @param ccCaigouMain 采购
     * @return 结果
     */
    @Override
    @Transactional
    public int updateCcCaigouMain(CcCaigouMain ccCaigouMain)
    {
        CcCaigouDetail info=new CcCaigouDetail();
        info.setCaigouDanhao(ccCaigouMain.getCaigouDanhao());
        List<CcCaigouDetail> oldInfoList = detailMapper.selectCcCaigouDetailList(info);
        for (CcCaigouDetail oldInfo:oldInfoList){
            detailMapper.deleteCcCaigouDetailById(oldInfo.getId());
        }
        List<CcCaigouDetail> newInfoList = ccCaigouMain.getInfoList();
        for (CcCaigouDetail newInfo:newInfoList){
            BigDecimal cgNum = newInfo.getCaigouNum();
            newInfo.setTotalPrice(newInfo.getUnitPrice().multiply(cgNum).setScale(2,BigDecimal.ROUND_HALF_UP));
            newInfo.setCreateTime(DateUtils.getNowDate());
            newInfo.setCreateBy(SecurityUtils.getUsername());
            newInfo.setCaigouDanhao(ccCaigouMain.getCaigouDanhao());
            if (ccCaigouMain.getShenheState().longValue()==1){
                newInfo.setIsRk(1L);
            }
            detailMapper.insertCcCaigouDetail(newInfo);
        }
        Map<String, String> map = detailMapper.tJPartsPurchaseInfo(ccCaigouMain.getCaigouDanhao());
        ccCaigouMain.setTotalNum(new BigDecimal(String.valueOf(map.get("cgNum"))));
        if (ccCaigouMain.getShenheState().longValue()==1){
            ccCaigouMain.setIsRk(1L);
            ccCaigouMain.setCaigouState("2");
            ccCaigouMain.setTotalPrice(new BigDecimal(String.valueOf(map.get("totalPrice"))));
            String number= "RK-"+NewNumber.createNewNumber(ccRukuMainMapper.getNewCode(DateUtils.getDate()));
            for (CcCaigouDetail newInfo:newInfoList){
                //入库单明细表
                CcRukuDetail rukuDetail=new CcRukuDetail();
                rukuDetail.setDanhao(number);
                rukuDetail.setRukuNum(newInfo.getCaigouNum());
                rukuDetail.setItemName(newInfo.getItemName());
                rukuDetail.setItemId(newInfo.getItemCode());
                rukuDetail.setSupplyId(newInfo.getSupplyId());
                rukuDetail.setSupplyName(newInfo.getSupplyName());
                rukuDetail.setPrice(newInfo.getTotalPrice());
                rukuDetail.setClassId(newInfo.getClassId());
                rukuDetail.setClassName(newInfo.getClassName());
                rukuDetail.setCreateTime(DateUtils.getNowDate());
                rukuDetail.setCreateBy(SecurityUtils.getUsername());
                rukuDetail.setUnit(newInfo.getUnit());
                rukuDetail.setSpec(newInfo.getSpec());
                ccRukuDetailMapper.insertCcRukuDetail(rukuDetail);
            }
            /*入库单主表*/
            CcRukuMain ccRukuMain=new CcRukuMain();
            ccRukuMain.setDanhao(number);
            ccRukuMain.setGldanhao(ccCaigouMain.getCaigouDanhao());
            ccRukuMain.setRukuType(0L);
            ccRukuMain.setCreateTime(DateUtils.getNowDate());
            ccRukuMain.setCreateBy(SecurityUtils.getUsername());
            ccRukuMain.setTotalNum(ccCaigouMain.getTotalNum());
            ccRukuMain.setDeptId(SecurityUtils.getDeptId());
            ccRukuMainMapper.insertCcRukuMain(ccRukuMain);
        }

        ccCaigouMainMapper.updateCcCaigouMain(ccCaigouMain);
        return 1;
    }
    @Override
    public int updateCaigouMainState(CcCaigouMain ccCaigouMain) {
        CcCaigouMain main=new CcCaigouMain();
        main.setId(ccCaigouMain.getId());
        main.setCaigouState(ccCaigouMain.getCaigouState());
        return  ccCaigouMainMapper.updateCcCaigouMain(main);
    }

    /*手机端审核*/
    @Override
    @Transactional
    public int updateMobileCcCaigouMain(CcCaigouMain ccCaigouMain) {
        ccCaigouMain.setIsRk(0L);
        if (ccCaigouMain.getShenheState().longValue()==1){
            CcCaigouDetail info=new CcCaigouDetail();
            info.setCaigouDanhao(ccCaigouMain.getCaigouDanhao());
            List<CcCaigouDetail> oldInfoList = detailMapper.selectCcCaigouDetailList(info);
            for (CcCaigouDetail oldInfo:oldInfoList){
                detailMapper.deleteCcCaigouDetailById(oldInfo.getId());
            }
           // String number= "RK-"+NewNumber.createNewNumber(ccRukuMainMapper.getNewCode(DateUtils.getDate()));
            List<CcCaigouDetail> newInfoList = ccCaigouMain.getInfoList();
            for (CcCaigouDetail newInfo:newInfoList){
                //重新生成采购单明细
                BigDecimal cgNum = newInfo.getCaigouNum();
                newInfo.setTotalPrice(newInfo.getUnitPrice().multiply(cgNum).setScale(2,BigDecimal.ROUND_HALF_UP));
                newInfo.setCreateTime(DateUtils.getNowDate());
                newInfo.setCreateBy(SecurityUtils.getUsername());
                newInfo.setCaigouDanhao(ccCaigouMain.getCaigouDanhao());
                detailMapper.insertCcCaigouDetail(newInfo);
                /*//入库单明细表
                CcRukuDetail rukuDetail=new CcRukuDetail();
                rukuDetail.setDanhao(number);
                rukuDetail.setRukuNum(newInfo.getCaigouNum());
                rukuDetail.setItemName(newInfo.getItemName());
                rukuDetail.setItemId(newInfo.getItemCode());
                rukuDetail.setSupplyId(newInfo.getSupplyId());
                rukuDetail.setSupplyName(newInfo.getSupplyName());
                rukuDetail.setPrice(newInfo.getTotalPrice());
                rukuDetail.setClassId(newInfo.getClassId());
                rukuDetail.setClassName(newInfo.getClassName());
                rukuDetail.setCreateTime(DateUtils.getNowDate());
                rukuDetail.setCreateBy(SecurityUtils.getUsername());
                rukuDetail.setUnit(newInfo.getUnit());
                rukuDetail.setSpec(newInfo.getSpec());
                ccRukuDetailMapper.insertCcRukuDetail(rukuDetail);*/
            }
            //重新修改采购单
            Map<String, String> map = detailMapper.tJPartsPurchaseInfo(ccCaigouMain.getCaigouDanhao());
            ccCaigouMain.setTotalPrice(new BigDecimal(String.valueOf(map.get("totalPrice"))));
            ccCaigouMain.setTotalNum(new BigDecimal(String.valueOf(map.get("cgNum"))));
            ccCaigouMain.setCaigouState("1");
            ccCaigouMainMapper.updateCcCaigouMain(ccCaigouMain);
          /*  *//*入库单主表*//*
            CcRukuMain ccRukuMain=new CcRukuMain();
            ccRukuMain.setDanhao(number);
            ccRukuMain.setGldanhao(ccCaigouMain.getCaigouDanhao());
            ccRukuMain.setRukuType(0L);
            ccRukuMain.setCreateTime(DateUtils.getNowDate());
            ccRukuMain.setCreateBy(SecurityUtils.getUsername());
            ccRukuMain.setTotalNum(ccCaigouMain.getTotalNum());
            ccRukuMain.setDeptId(SecurityUtils.getDeptId());
            ccRukuMainMapper.insertCcRukuMain(ccRukuMain);*/
        }else{
            ccCaigouMainMapper.updateCcCaigouMain(ccCaigouMain);
        }
        return 1;
    }

    /**
     * 批量删除采购
     * 
     * @param ids 需要删除的采购主键
     * @return 结果
     */
    @Override
    public int deleteCcCaigouMainByIds(Long[] ids)
    {
        return ccCaigouMainMapper.deleteCcCaigouMainByIds(ids);
    }

    /**
     * 删除采购信息
     * 
     * @param id 采购主键
     * @return 结果
     */
    @Override
    public int deleteCcCaigouMainById(Long id)
    {
        return ccCaigouMainMapper.deleteCcCaigouMainById(id);
    }

    @Override
    @Transactional
    public int deleteCcCaigouMainByDanhao(String danhao) {
        ccCaigouMainMapper.deleteCcCaigouMainByDanhao(danhao);
        detailMapper.deleteCcCaigouDetailByDanhao(danhao);
        return 1;
    }
}
